package cn.ekuma.epos.datalogic.define.dao.sale.kitchen;

import java.util.Date;
import java.util.List;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.util.JsonUtil;

import com.openbravo.bean.Product;
import com.openbravo.bean.sales.KitchenWorkLine;
import com.openbravo.bean.sales.Ticket;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.SerializerWriteInteger;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class KitchenWorkLineDAO extends ModifiedLogDAO<KitchenWorkLine>{

	public KitchenWorkLineDAO(I_Session s) {
		super(s);
	}


	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "KITCHEN_WORKLINE",
                new Field[]{
				     new Field("ID",Datas.STRING,Formats.STRING),
                     new Field("TICKET",Datas.STRING,Formats.STRING,Ticket.class),
                     new Field("LINE",Datas.INT,Formats.INT),
                     new Field("PRODUCT",Datas.STRING,Formats.STRING,Product.class),
                     new Field("ATTRIBUTESETINSTANCE_ID",Datas.STRING,Formats.STRING),
                     new Field("UNITS",Datas.DOUBLE,Formats.DOUBLE),
                     new Field("ATTRIBUTES",Datas.STRING,Formats.STRING),
                     new Field("CURDATE",Datas.TIMESTAMP,Formats.TIMESTAMP),
                     new Field("HOSTNAME",Datas.STRING,Formats.STRING),
                     new Field("ORDERNUM",Datas.INT,Formats.INT),
                     new Field("TICKETSTATE",Datas.INT,Formats.INT),
                     new Field("LINESTATE",Datas.INT,Formats.INT),
                     new Field("PLACE",Datas.STRING,Formats.STRING),
                     new Field("URGENT",Datas.BOOLEAN,Formats.BOOLEAN),
                     new Field("LASTMODIFIED",Datas.TIMESTAMP,Formats.TIMESTAMP)
                    },
                new int[]{0});
	}
	
	@Override
	public KitchenWorkLine readValues(DataRead dr,KitchenWorkLine obj) throws BasicException {
		if(obj==null)
			obj=new KitchenWorkLine();
		obj.setId(dr.getString(1));
		obj.setTicketId(dr.getString(2));
		obj.setTicketLineId(dr.getInt(3));
		obj.setProductid(dr.getString(4));
		obj.setAttsetinstid(dr.getString(5));
		
		obj.setMultiply(dr.getDouble(6));
		obj.setAttributes(JsonUtil.getHashMap(dr.getString(7)));
		obj.setCreatDate(dr.getTimestamp(8));
		obj.setHostName(dr.getString(9));
		obj.setOrder(dr.getInt(10));
		
		obj.setTicketState(dr.getInt(11));
		obj.setLineState(dr.getInt(12));
		obj.setTableName(dr.getString(13));
		obj.setUrgent(dr.getBoolean(14));
		obj.setLastModified(dr.getTimestamp(15));
		return obj;
	}

	@Override
	public void writeInsertValues(DataWrite dp, KitchenWorkLine obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(1,obj.getTicketId());
		dp.setInt(3,obj.getTicketLineId());
		dp.setString(4,obj.getProductid());
		dp.setString(5,obj.getAttsetinstid());
		
		dp.setDouble(6,obj.getMultiply());
		dp.setString(7,JsonUtil.toString(obj.getAttributes()));
		dp.setTimestamp(8,obj.getCreatDate());
		dp.setString(9,obj.getHostName());
		dp.setInt(10,obj.getOrder());
		
		dp.setInt(11,obj.getTicketState());
		dp.setInt(12,obj.getLineState());
		dp.setString(13,obj.getTableName());
		dp.setBoolean(14,obj.isUrgent());
		dp.setTimestamp(15,obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return KitchenWorkLine.class;
	}
	
	public void cooking(String id,String host,int order)throws BasicException {
		updateLineState(id,host,order,KitchenWorkLine.LINESTATE_cooking);
	}
	
	
	public void backForWait(String id)throws BasicException {
		updateLineState(id,null,0,KitchenWorkLine.LINESTATE_wait);
	}
	
	public void updateLineState(final String id,final String host,final int order,final int state)throws BasicException {
		 new StaticSentence(s
                , "UPDATE KITCHEN_WORKLINE SET HOSTNAME=? AND ORDERNUM=? AND LINESTATE=? WHERE ID=?"
                , SerializerWriteParams.INSTANCE
                , null).exec(new DataParams() {
                    public void writeValues() throws BasicException {
                    	setString(1, host);
                    	setInt(2, order);
                    	setInt(3,state);
                    	setString(4, id);
                    }});
	}
	
	
	public void updateTicketState(final String ticketId,final int state)throws BasicException {
		new StaticSentence(s
                , "UPDATE KITCHEN_WORKLINE SET  TICKETSTATE=? AND LASTMODIFIED=? WHERE TICKET=?"
                , SerializerWriteParams.INSTANCE
                , null).exec(new DataParams() {
                    public void writeValues() throws BasicException {
                    	setInt(1, state);
                    	setTimestamp(2, new Date());
                        setString(3, ticketId);
                    }});
	}
	
	public void ticketDelete(String ticketId)throws BasicException {
		new StaticSentence(s
                , "DELETE KITCHEN_WORKLINE WHERE TICKET=? "
                , SerializerWriteString.INSTANCE
                , null).exec(ticketId);
	}
	
   public void urgentToKitch(String ticketId)throws BasicException {
	   new StaticSentence(s
               , "UPDATE KITCHEN_WORKLINE SET URGENT="+s.getDB().TRUE()+" WHERE TICKET=? AND LINESTATE="+KitchenWorkLine.LINESTATE_wait
               , SerializerWriteString.INSTANCE
               , null).exec(ticketId);
    }
   
   public List<KitchenWorkLine> listKitchenWorkLineByLineState(int lineState)throws BasicException {
	  return  new StaticSentence(s,this.getTableDefinition().getListSQL()+" WHERE LINESTATE=?" 
			   , SerializerWriteInteger.INSTANCE
               , this).list(lineState);
   }
   
   public List<KitchenWorkLine> listKitchenWorkLineByHostCooking(String host)throws BasicException {
	   return  new StaticSentence(s,this.getTableDefinition().getListSQL()+" WHERE HOSTNAME=? AND LINESTATE="+KitchenWorkLine.LINESTATE_cooking
			   , SerializerWriteString.INSTANCE
               , this).list(host);
   }


public List<KitchenWorkLine> listKitchenWorkLineByTicketID(String ticketId) throws BasicException {
	return  new StaticSentence(s,this.getTableDefinition().getListSQL()+" WHERE TICKET=?"
			   , SerializerWriteString.INSTANCE
            , this).list(ticketId);
}
}
